package com.bien.springboot.configuration;

import com.bien.springboot.filter.CodeFilter;
import com.bien.springboot.listener.CodeListener;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class WebConfiguration {

    @Bean
    public FilterRegistrationBean webAppForIndexFilterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setName("CodeFilter");
        CodeFilter codeFilter = new CodeFilter();
        registrationBean.setFilter(codeFilter);
        registrationBean.setOrder(4);
        List<String> urlList = new ArrayList<>();
        urlList.add("/*");
        registrationBean.setUrlPatterns(urlList);
        return registrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean<CodeListener> servletListenerRegistrationBean() {
        ServletListenerRegistrationBean<CodeListener> servletListenerRegistrationBean = new ServletListenerRegistrationBean<>();
        servletListenerRegistrationBean.setListener(new CodeListener());
        return servletListenerRegistrationBean;
    }
}
